1 // SPDX-License-Identifier: GPL-2.0+
3 * VMEbus User access driver
5 * Author: Martyn Welch <martyn.welch@ge.com>
6 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
9 * Tom Armistead and Ajit Prem
10 * Copyright 2004 Motorola Inc.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/refcount.h>
16 #include <linux/cdev.h>
17 #include <linux/delay.h>
18 #include <linux/device.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/errno.h>
21 #include <linux/init.h>
22 #include <linux/ioctl.h>
23 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/pagemap.h>
27 #include <linux/pci.h>
28 #include <linux/mutex.h>
29 #include <linux/slab.h>
30 #include <linux/spinlock.h>
31 #include <linux/syscalls.h>
32 #include <linux/types.h>
35 #include <linux/uaccess.h>
36 #include <linux/vme.h>
40 static const char driver_name[] = "vme_user";
42 static int bus[VME_USER_BUS_MAX];
43 static unsigned int bus_num;
45 /* Currently Documentation/admin-guide/devices.rst defines the
49 * 0 = /dev/bus/vme/m0 First master image
50 * 1 = /dev/bus/vme/m1 Second master image
51 * 2 = /dev/bus/vme/m2 Third master image
52 * 3 = /dev/bus/vme/m3 Fourth master image
53 * 4 = /dev/bus/vme/s0 First slave image
54 * 5 = /dev/bus/vme/s1 Second slave image
55 * 6 = /dev/bus/vme/s2 Third slave image
56 * 7 = /dev/bus/vme/s3 Fourth slave image
57 * 8 = /dev/bus/vme/ctl Control
59 * It is expected that all VME bus drivers will use the
60 * same interface. For interface documentation see
61 * http://www.vmelinux.org/.
63 * However the VME driver at http://www.vmelinux.org/ is rather old and doesn't
64 * even support the tsi148 chipset (which has 8 master and 8 slave windows).
65 * We'll run with this for now as far as possible, however it probably makes
66 * sense to get rid of the old mappings and just do everything dynamically.
68 * So for now, we'll restrict the driver to providing 4 masters and 4 slaves as
69 * defined above and try to support at least some of the interface from
70 * http://www.vmelinux.org/ as an alternative the driver can be written
71 * providing a saner interface later.
73 * The vmelinux.org driver never supported slave images, the devices reserved
74 * for slaves were repurposed to support all 8 master images on the UniverseII!
75 * We shall support 4 masters and 4 slaves with this driver.
77 #define VME_MAJOR 221 /* VME Major Device Number */
78 #define VME_DEVS 9 /* Number of dev entries */
80 #define MASTER_MINOR 0
84 #define CONTROL_MINOR 8
86 #define PCI_BUF_SIZE 0x20000 /* Size of one slave image buffer */
89 * Structure to handle image related parameters.
92 void *kern_buf; /* Buffer address in kernel space */
93 dma_addr_t pci_buf; /* Buffer address in PCI address space */
94 unsigned long long size_buf; /* Buffer size */
95 struct mutex mutex; /* Mutex for locking image */
96 struct device *device; /* Sysfs device */
97 struct vme_resource *resource; /* VME resource */
98 int mmap_count; /* Number of current mmap's */
101 static struct image_desc image[VME_DEVS];
103 static struct cdev *vme_user_cdev; /* Character device */
104 static struct class *vme_user_sysfs_class; /* Sysfs class */
105 static struct vme_dev *vme_user_bridge; /* Pointer to user device */
107 static const int type[VME_DEVS] = { MASTER_MINOR, MASTER_MINOR,
108 MASTER_MINOR, MASTER_MINOR,
109 SLAVE_MINOR, SLAVE_MINOR,
110 SLAVE_MINOR, SLAVE_MINOR,
114 struct vme_user_vma_priv {
119 static ssize_t resource_to_user(int minor, char __user *buf, size_t count,
124 if (count > image[minor].size_buf)
125 count = image[minor].size_buf;
127 copied = vme_master_read(image[minor].resource, image[minor].kern_buf,
132 if (copy_to_user(buf, image[minor].kern_buf, (unsigned long)copied))
138 static ssize_t resource_from_user(unsigned int minor, const char __user *buf,
139 size_t count, loff_t *ppos)
141 if (count > image[minor].size_buf)
142 count = image[minor].size_buf;
144 if (copy_from_user(image[minor].kern_buf, buf, (unsigned long)count))
147 return vme_master_write(image[minor].resource, image[minor].kern_buf,
151 static ssize_t buffer_to_user(unsigned int minor, char __user *buf,
152 size_t count, loff_t *ppos)
156 image_ptr = image[minor].kern_buf + *ppos;
157 if (copy_to_user(buf, image_ptr, (unsigned long)count))
163 static ssize_t buffer_from_user(unsigned int minor, const char __user *buf,
164 size_t count, loff_t *ppos)
168 image_ptr = image[minor].kern_buf + *ppos;
169 if (copy_from_user(image_ptr, buf, (unsigned long)count))
175 static ssize_t vme_user_read(struct file *file, char __user *buf, size_t count,
178 unsigned int minor = iminor(file_inode(file));
182 if (minor == CONTROL_MINOR)
185 mutex_lock(&image[minor].mutex);
187 /* XXX Do we *really* want this helper - we can use vme_*_get ? */
188 image_size = vme_get_size(image[minor].resource);
190 /* Ensure we are starting at a valid location */
191 if ((*ppos < 0) || (*ppos > (image_size - 1))) {
192 mutex_unlock(&image[minor].mutex);
196 /* Ensure not reading past end of the image */
197 if (*ppos + count > image_size)
198 count = image_size - *ppos;
200 switch (type[minor]) {
202 retval = resource_to_user(minor, buf, count, ppos);
205 retval = buffer_to_user(minor, buf, count, ppos);
211 mutex_unlock(&image[minor].mutex);
218 static ssize_t vme_user_write(struct file *file, const char __user *buf,
219 size_t count, loff_t *ppos)
221 unsigned int minor = iminor(file_inode(file));
225 if (minor == CONTROL_MINOR)
228 mutex_lock(&image[minor].mutex);
230 image_size = vme_get_size(image[minor].resource);
232 /* Ensure we are starting at a valid location */
233 if ((*ppos < 0) || (*ppos > (image_size - 1))) {
234 mutex_unlock(&image[minor].mutex);
238 /* Ensure not reading past end of the image */
239 if (*ppos + count > image_size)
240 count = image_size - *ppos;
242 switch (type[minor]) {
244 retval = resource_from_user(minor, buf, count, ppos);
247 retval = buffer_from_user(minor, buf, count, ppos);
253 mutex_unlock(&image[minor].mutex);
261 static loff_t vme_user_llseek(struct file *file, loff_t off, int whence)
263 unsigned int minor = iminor(file_inode(file));
267 switch (type[minor]) {
270 mutex_lock(&image[minor].mutex);
271 image_size = vme_get_size(image[minor].resource);
272 res = fixed_size_llseek(file, off, whence, image_size);
273 mutex_unlock(&image[minor].mutex);
281 * The ioctls provided by the old VME access method (the one at vmelinux.org)
282 * are most certainly wrong as the effectively push the registers layout
283 * through to user space. Given that the VME core can handle multiple bridges,
284 * with different register layouts this is most certainly not the way to go.
286 * We aren't using the structures defined in the Motorola driver either - these
287 * are also quite low level, however we should use the definitions that have
288 * already been defined.
290 static int vme_user_ioctl(struct inode *inode, struct file *file,
291 unsigned int cmd, unsigned long arg)
293 struct vme_master master;
294 struct vme_slave slave;
295 struct vme_irq_id irq_req;
296 unsigned long copied;
297 unsigned int minor = iminor(inode);
300 void __user *argp = (void __user *)arg;
302 switch (type[minor]) {
306 copied = copy_from_user(&irq_req, argp,
309 pr_warn("Partial copy from userspace\n");
313 return vme_irq_generate(vme_user_bridge,
321 memset(&master, 0, sizeof(master));
323 /* XXX We do not want to push aspace, cycle and width
324 * to userspace as they are
326 retval = vme_master_get(image[minor].resource,
329 &master.size, &master.aspace,
330 &master.cycle, &master.dwidth);
332 copied = copy_to_user(argp, &master,
335 pr_warn("Partial copy to userspace\n");
343 if (image[minor].mmap_count != 0) {
344 pr_warn("Can't adjust mapped window\n");
348 copied = copy_from_user(&master, argp, sizeof(master));
350 pr_warn("Partial copy from userspace\n");
354 /* XXX We do not want to push aspace, cycle and width
355 * to userspace as they are
357 return vme_master_set(image[minor].resource,
358 master.enable, master.vme_addr, master.size,
359 master.aspace, master.cycle, master.dwidth);
367 memset(&slave, 0, sizeof(slave));
369 /* XXX We do not want to push aspace, cycle and width
370 * to userspace as they are
372 retval = vme_slave_get(image[minor].resource,
373 &slave.enable, &slave.vme_addr,
374 &slave.size, &pci_addr,
375 &slave.aspace, &slave.cycle);
377 copied = copy_to_user(argp, &slave,
380 pr_warn("Partial copy to userspace\n");
388 copied = copy_from_user(&slave, argp, sizeof(slave));
390 pr_warn("Partial copy from userspace\n");
394 /* XXX We do not want to push aspace, cycle and width
395 * to userspace as they are
397 return vme_slave_set(image[minor].resource,
398 slave.enable, slave.vme_addr, slave.size,
399 image[minor].pci_buf, slave.aspace,
411 vme_user_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
414 struct inode *inode = file_inode(file);
415 unsigned int minor = iminor(inode);
417 mutex_lock(&image[minor].mutex);
418 ret = vme_user_ioctl(inode, file, cmd, arg);
419 mutex_unlock(&image[minor].mutex);
424 static void vme_user_vm_open(struct vm_area_struct *vma)
426 struct vme_user_vma_priv *vma_priv = vma->vm_private_data;
428 refcount_inc(&vma_priv->refcnt);
431 static void vme_user_vm_close(struct vm_area_struct *vma)
433 struct vme_user_vma_priv *vma_priv = vma->vm_private_data;
434 unsigned int minor = vma_priv->minor;
436 if (!refcount_dec_and_test(&vma_priv->refcnt))
439 mutex_lock(&image[minor].mutex);
440 image[minor].mmap_count--;
441 mutex_unlock(&image[minor].mutex);
446 static const struct vm_operations_struct vme_user_vm_ops = {
447 .open = vme_user_vm_open,
448 .close = vme_user_vm_close,
451 static int vme_user_master_mmap(unsigned int minor, struct vm_area_struct *vma)
454 struct vme_user_vma_priv *vma_priv;
456 mutex_lock(&image[minor].mutex);
458 err = vme_master_mmap(image[minor].resource, vma);
460 mutex_unlock(&image[minor].mutex);
464 vma_priv = kmalloc(sizeof(*vma_priv), GFP_KERNEL);
466 mutex_unlock(&image[minor].mutex);
470 vma_priv->minor = minor;
471 refcount_set(&vma_priv->refcnt, 1);
472 vma->vm_ops = &vme_user_vm_ops;
473 vma->vm_private_data = vma_priv;
475 image[minor].mmap_count++;
477 mutex_unlock(&image[minor].mutex);
482 static int vme_user_mmap(struct file *file, struct vm_area_struct *vma)
484 unsigned int minor = iminor(file_inode(file));
486 if (type[minor] == MASTER_MINOR)
487 return vme_user_master_mmap(minor, vma);
492 static const struct file_operations vme_user_fops = {
493 .read = vme_user_read,
494 .write = vme_user_write,
495 .llseek = vme_user_llseek,
496 .unlocked_ioctl = vme_user_unlocked_ioctl,
497 .compat_ioctl = compat_ptr_ioctl,
498 .mmap = vme_user_mmap,
501 static int vme_user_match(struct vme_dev *vdev)
505 int cur_bus = vme_bus_num(vdev);
506 int cur_slot = vme_slot_num(vdev);
508 for (i = 0; i < bus_num; i++)
509 if ((cur_bus == bus[i]) && (cur_slot == vdev->num))
516 * In this simple access driver, the old behaviour is being preserved as much
517 * as practical. We will therefore reserve the buffers and request the images
518 * here so that we don't have to do it later.
520 static int vme_user_probe(struct vme_dev *vdev)
525 /* Save pointer to the bridge device */
526 if (vme_user_bridge) {
527 dev_err(&vdev->dev, "Driver can only be loaded for 1 device\n");
531 vme_user_bridge = vdev;
533 /* Initialise descriptors */
534 for (i = 0; i < VME_DEVS; i++) {
535 image[i].kern_buf = NULL;
536 image[i].pci_buf = 0;
537 mutex_init(&image[i].mutex);
538 image[i].device = NULL;
539 image[i].resource = NULL;
542 /* Assign major and minor numbers for the driver */
543 err = register_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS,
546 dev_warn(&vdev->dev, "Error getting Major Number %d for driver.\n",
551 /* Register the driver as a char device */
552 vme_user_cdev = cdev_alloc();
553 if (!vme_user_cdev) {
557 vme_user_cdev->ops = &vme_user_fops;
558 vme_user_cdev->owner = THIS_MODULE;
559 err = cdev_add(vme_user_cdev, MKDEV(VME_MAJOR, 0), VME_DEVS);
563 /* Request slave resources and allocate buffers (128kB wide) */
564 for (i = SLAVE_MINOR; i < (SLAVE_MAX + 1); i++) {
565 /* XXX Need to properly request attributes */
566 /* For ca91cx42 bridge there are only two slave windows
567 * supporting A16 addressing, so we request A24 supported
570 image[i].resource = vme_slave_request(vme_user_bridge,
572 if (!image[i].resource) {
574 "Unable to allocate slave resource\n");
578 image[i].size_buf = PCI_BUF_SIZE;
579 image[i].kern_buf = vme_alloc_consistent(image[i].resource,
582 if (!image[i].kern_buf) {
584 "Unable to allocate memory for buffer\n");
585 image[i].pci_buf = 0;
586 vme_slave_free(image[i].resource);
593 * Request master resources allocate page sized buffers for small
596 for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++) {
597 /* XXX Need to properly request attributes */
598 image[i].resource = vme_master_request(vme_user_bridge,
601 if (!image[i].resource) {
603 "Unable to allocate master resource\n");
607 image[i].size_buf = PCI_BUF_SIZE;
608 image[i].kern_buf = kmalloc(image[i].size_buf, GFP_KERNEL);
609 if (!image[i].kern_buf) {
611 vme_master_free(image[i].resource);
616 /* Create sysfs entries - on udev systems this creates the dev files */
617 vme_user_sysfs_class = class_create(THIS_MODULE, driver_name);
618 if (IS_ERR(vme_user_sysfs_class)) {
619 dev_err(&vdev->dev, "Error creating vme_user class.\n");
620 err = PTR_ERR(vme_user_sysfs_class);
624 /* Add sysfs Entries */
625 for (i = 0; i < VME_DEVS; i++) {
630 name = "bus/vme/m%d";
633 name = "bus/vme/ctl";
636 name = "bus/vme/s%d";
643 num = (type[i] == SLAVE_MINOR) ? i - (MASTER_MAX + 1) : i;
644 image[i].device = device_create(vme_user_sysfs_class, NULL,
645 MKDEV(VME_MAJOR, i), NULL,
647 if (IS_ERR(image[i].device)) {
648 dev_info(&vdev->dev, "Error creating sysfs device\n");
649 err = PTR_ERR(image[i].device);
659 device_destroy(vme_user_sysfs_class, MKDEV(VME_MAJOR, i));
661 class_destroy(vme_user_sysfs_class);
663 /* Ensure counter set correctly to unalloc all master windows */
666 while (i > MASTER_MINOR) {
668 kfree(image[i].kern_buf);
669 vme_master_free(image[i].resource);
673 * Ensure counter set correctly to unalloc all slave windows and buffers
677 while (i > SLAVE_MINOR) {
679 vme_free_consistent(image[i].resource, image[i].size_buf,
680 image[i].kern_buf, image[i].pci_buf);
681 vme_slave_free(image[i].resource);
684 cdev_del(vme_user_cdev);
686 unregister_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS);
692 static int vme_user_remove(struct vme_dev *dev)
696 /* Remove sysfs Entries */
697 for (i = 0; i < VME_DEVS; i++) {
698 mutex_destroy(&image[i].mutex);
699 device_destroy(vme_user_sysfs_class, MKDEV(VME_MAJOR, i));
701 class_destroy(vme_user_sysfs_class);
703 for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++) {
704 kfree(image[i].kern_buf);
705 vme_master_free(image[i].resource);
708 for (i = SLAVE_MINOR; i < (SLAVE_MAX + 1); i++) {
709 vme_slave_set(image[i].resource, 0, 0, 0, 0, VME_A32, 0);
710 vme_free_consistent(image[i].resource, image[i].size_buf,
711 image[i].kern_buf, image[i].pci_buf);
712 vme_slave_free(image[i].resource);
715 /* Unregister device driver */
716 cdev_del(vme_user_cdev);
718 /* Unregister the major and minor device numbers */
719 unregister_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS);
724 static struct vme_driver vme_user_driver = {
726 .match = vme_user_match,
727 .probe = vme_user_probe,
728 .remove = vme_user_remove,
731 static int __init vme_user_init(void)
735 pr_info("VME User Space Access Driver\n");
738 pr_err("No cards, skipping registration\n");
743 /* Let's start by supporting one bus, we can support more than one
744 * in future revisions if that ever becomes necessary.
746 if (bus_num > VME_USER_BUS_MAX) {
747 pr_err("Driver only able to handle %d buses\n",
749 bus_num = VME_USER_BUS_MAX;
753 * Here we just register the maximum number of devices we can and
754 * leave vme_user_match() to allow only 1 to go through to probe().
755 * This way, if we later want to allow multiple user access devices,
756 * we just change the code in vme_user_match().
758 retval = vme_register_driver(&vme_user_driver, VME_MAX_SLOTS);
769 static void __exit vme_user_exit(void)
771 vme_unregister_driver(&vme_user_driver);
774 MODULE_PARM_DESC(bus, "Enumeration of VMEbus to which the driver is connected");
775 module_param_array(bus, int, &bus_num, 0000);
777 MODULE_DESCRIPTION("VME User Space Access Driver");
778 MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com");
779 MODULE_LICENSE("GPL");
781 module_init(vme_user_init);
782 module_exit(vme_user_exit);