2 * VMEbus User access driver
4 * Author: Martyn Welch <martyn.welch@ge.com>
5 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
8 * Tom Armistead and Ajit Prem
9 * Copyright 2004 Motorola Inc.
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/atomic.h>
21 #include <linux/cdev.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/errno.h>
26 #include <linux/init.h>
27 #include <linux/ioctl.h>
28 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/pagemap.h>
32 #include <linux/pci.h>
33 #include <linux/mutex.h>
34 #include <linux/slab.h>
35 #include <linux/spinlock.h>
36 #include <linux/syscalls.h>
37 #include <linux/types.h>
40 #include <linux/uaccess.h>
41 #include <linux/vme.h>
45 static const char driver_name[] = "vme_user";
47 static int bus[VME_USER_BUS_MAX];
48 static unsigned int bus_num;
50 /* Currently Documentation/devices.txt defines the following for VME:
53 * 0 = /dev/bus/vme/m0 First master image
54 * 1 = /dev/bus/vme/m1 Second master image
55 * 2 = /dev/bus/vme/m2 Third master image
56 * 3 = /dev/bus/vme/m3 Fourth master image
57 * 4 = /dev/bus/vme/s0 First slave image
58 * 5 = /dev/bus/vme/s1 Second slave image
59 * 6 = /dev/bus/vme/s2 Third slave image
60 * 7 = /dev/bus/vme/s3 Fourth slave image
61 * 8 = /dev/bus/vme/ctl Control
63 * It is expected that all VME bus drivers will use the
64 * same interface. For interface documentation see
65 * http://www.vmelinux.org/.
67 * However the VME driver at http://www.vmelinux.org/ is rather old and doesn't
68 * even support the tsi148 chipset (which has 8 master and 8 slave windows).
69 * We'll run with this for now as far as possible, however it probably makes
70 * sense to get rid of the old mappings and just do everything dynamically.
72 * So for now, we'll restrict the driver to providing 4 masters and 4 slaves as
73 * defined above and try to support at least some of the interface from
74 * http://www.vmelinux.org/ as an alternative the driver can be written
75 * providing a saner interface later.
77 * The vmelinux.org driver never supported slave images, the devices reserved
78 * for slaves were repurposed to support all 8 master images on the UniverseII!
79 * We shall support 4 masters and 4 slaves with this driver.
81 #define VME_MAJOR 221 /* VME Major Device Number */
82 #define VME_DEVS 9 /* Number of dev entries */
84 #define MASTER_MINOR 0
88 #define CONTROL_MINOR 8
90 #define PCI_BUF_SIZE 0x20000 /* Size of one slave image buffer */
93 * Structure to handle image related parameters.
96 void *kern_buf; /* Buffer address in kernel space */
97 dma_addr_t pci_buf; /* Buffer address in PCI address space */
98 unsigned long long size_buf; /* Buffer size */
99 struct mutex mutex; /* Mutex for locking image */
100 struct device *device; /* Sysfs device */
101 struct vme_resource *resource; /* VME resource */
102 int users; /* Number of current users */
103 int mmap_count; /* Number of current mmap's */
105 static struct image_desc image[VME_DEVS];
107 struct driver_stats {
109 unsigned long writes;
110 unsigned long ioctls;
113 unsigned long dmaerrors;
114 unsigned long timeouts;
115 unsigned long external;
117 static struct driver_stats statistics;
119 static struct cdev *vme_user_cdev; /* Character device */
120 static struct class *vme_user_sysfs_class; /* Sysfs class */
121 static struct vme_dev *vme_user_bridge; /* Pointer to user device */
124 static const int type[VME_DEVS] = { MASTER_MINOR, MASTER_MINOR,
125 MASTER_MINOR, MASTER_MINOR,
126 SLAVE_MINOR, SLAVE_MINOR,
127 SLAVE_MINOR, SLAVE_MINOR,
132 static int vme_user_open(struct inode *, struct file *);
133 static int vme_user_release(struct inode *, struct file *);
134 static ssize_t vme_user_read(struct file *, char __user *, size_t, loff_t *);
135 static ssize_t vme_user_write(struct file *, const char __user *, size_t,
137 static loff_t vme_user_llseek(struct file *, loff_t, int);
138 static long vme_user_unlocked_ioctl(struct file *, unsigned int, unsigned long);
139 static int vme_user_mmap(struct file *file, struct vm_area_struct *vma);
141 static void vme_user_vm_open(struct vm_area_struct *vma);
142 static void vme_user_vm_close(struct vm_area_struct *vma);
144 static int vme_user_match(struct vme_dev *);
145 static int vme_user_probe(struct vme_dev *);
146 static int vme_user_remove(struct vme_dev *);
148 static const struct file_operations vme_user_fops = {
149 .open = vme_user_open,
150 .release = vme_user_release,
151 .read = vme_user_read,
152 .write = vme_user_write,
153 .llseek = vme_user_llseek,
154 .unlocked_ioctl = vme_user_unlocked_ioctl,
155 .compat_ioctl = vme_user_unlocked_ioctl,
156 .mmap = vme_user_mmap,
159 struct vme_user_vma_priv {
164 static const struct vm_operations_struct vme_user_vm_ops = {
165 .open = vme_user_vm_open,
166 .close = vme_user_vm_close,
171 * Reset all the statistic counters
173 static void reset_counters(void)
175 statistics.reads = 0;
176 statistics.writes = 0;
177 statistics.ioctls = 0;
179 statistics.berrs = 0;
180 statistics.dmaerrors = 0;
181 statistics.timeouts = 0;
184 static int vme_user_open(struct inode *inode, struct file *file)
187 unsigned int minor = MINOR(inode->i_rdev);
189 mutex_lock(&image[minor].mutex);
190 /* Allow device to be opened if a resource is needed and allocated. */
191 if (minor < CONTROL_MINOR && image[minor].resource == NULL) {
192 pr_err("No resources allocated for device\n");
197 /* Increment user count */
198 image[minor].users++;
200 mutex_unlock(&image[minor].mutex);
205 mutex_unlock(&image[minor].mutex);
210 static int vme_user_release(struct inode *inode, struct file *file)
212 unsigned int minor = MINOR(inode->i_rdev);
214 mutex_lock(&image[minor].mutex);
216 /* Decrement user count */
217 image[minor].users--;
219 mutex_unlock(&image[minor].mutex);
225 * We are going ot alloc a page during init per window for small transfers.
226 * Small transfers will go VME -> buffer -> user space. Larger (more than a
227 * page) transfers will lock the user space buffer into memory and then
228 * transfer the data directly into the user space buffers.
230 static ssize_t resource_to_user(int minor, char __user *buf, size_t count,
236 if (count <= image[minor].size_buf) {
237 /* We copy to kernel buffer */
238 copied = vme_master_read(image[minor].resource,
239 image[minor].kern_buf, count, *ppos);
243 retval = __copy_to_user(buf, image[minor].kern_buf,
244 (unsigned long)copied);
246 copied = (copied - retval);
247 pr_info("User copy failed\n");
252 /* XXX Need to write this */
253 pr_info("Currently don't support large transfers\n");
254 /* Map in pages from userspace */
256 /* Call vme_master_read to do the transfer */
264 * We are going to alloc a page during init per window for small transfers.
265 * Small transfers will go user space -> buffer -> VME. Larger (more than a
266 * page) transfers will lock the user space buffer into memory and then
267 * transfer the data directly from the user space buffers out to VME.
269 static ssize_t resource_from_user(unsigned int minor, const char __user *buf,
270 size_t count, loff_t *ppos)
275 if (count <= image[minor].size_buf) {
276 retval = __copy_from_user(image[minor].kern_buf, buf,
277 (unsigned long)count);
279 copied = (copied - retval);
283 copied = vme_master_write(image[minor].resource,
284 image[minor].kern_buf, copied, *ppos);
286 /* XXX Need to write this */
287 pr_info("Currently don't support large transfers\n");
288 /* Map in pages from userspace */
290 /* Call vme_master_write to do the transfer */
297 static ssize_t buffer_to_user(unsigned int minor, char __user *buf,
298 size_t count, loff_t *ppos)
303 image_ptr = image[minor].kern_buf + *ppos;
305 retval = __copy_to_user(buf, image_ptr, (unsigned long)count);
307 retval = (count - retval);
308 pr_warn("Partial copy to userspace\n");
312 /* Return number of bytes successfully read */
316 static ssize_t buffer_from_user(unsigned int minor, const char __user *buf,
317 size_t count, loff_t *ppos)
322 image_ptr = image[minor].kern_buf + *ppos;
324 retval = __copy_from_user(image_ptr, buf, (unsigned long)count);
326 retval = (count - retval);
327 pr_warn("Partial copy to userspace\n");
331 /* Return number of bytes successfully read */
335 static ssize_t vme_user_read(struct file *file, char __user *buf, size_t count,
338 unsigned int minor = MINOR(file_inode(file)->i_rdev);
343 if (minor == CONTROL_MINOR)
346 mutex_lock(&image[minor].mutex);
348 /* XXX Do we *really* want this helper - we can use vme_*_get ? */
349 image_size = vme_get_size(image[minor].resource);
351 /* Ensure we are starting at a valid location */
352 if ((*ppos < 0) || (*ppos > (image_size - 1))) {
353 mutex_unlock(&image[minor].mutex);
357 /* Ensure not reading past end of the image */
358 if (*ppos + count > image_size)
359 okcount = image_size - *ppos;
363 switch (type[minor]) {
365 retval = resource_to_user(minor, buf, okcount, ppos);
368 retval = buffer_to_user(minor, buf, okcount, ppos);
374 mutex_unlock(&image[minor].mutex);
381 static ssize_t vme_user_write(struct file *file, const char __user *buf,
382 size_t count, loff_t *ppos)
384 unsigned int minor = MINOR(file_inode(file)->i_rdev);
389 if (minor == CONTROL_MINOR)
392 mutex_lock(&image[minor].mutex);
394 image_size = vme_get_size(image[minor].resource);
396 /* Ensure we are starting at a valid location */
397 if ((*ppos < 0) || (*ppos > (image_size - 1))) {
398 mutex_unlock(&image[minor].mutex);
402 /* Ensure not reading past end of the image */
403 if (*ppos + count > image_size)
404 okcount = image_size - *ppos;
408 switch (type[minor]) {
410 retval = resource_from_user(minor, buf, okcount, ppos);
413 retval = buffer_from_user(minor, buf, okcount, ppos);
419 mutex_unlock(&image[minor].mutex);
427 static loff_t vme_user_llseek(struct file *file, loff_t off, int whence)
429 unsigned int minor = MINOR(file_inode(file)->i_rdev);
433 if (minor == CONTROL_MINOR)
436 mutex_lock(&image[minor].mutex);
437 image_size = vme_get_size(image[minor].resource);
438 res = fixed_size_llseek(file, off, whence, image_size);
439 mutex_unlock(&image[minor].mutex);
445 * The ioctls provided by the old VME access method (the one at vmelinux.org)
446 * are most certainly wrong as the effectively push the registers layout
447 * through to user space. Given that the VME core can handle multiple bridges,
448 * with different register layouts this is most certainly not the way to go.
450 * We aren't using the structures defined in the Motorola driver either - these
451 * are also quite low level, however we should use the definitions that have
452 * already been defined.
454 static int vme_user_ioctl(struct inode *inode, struct file *file,
455 unsigned int cmd, unsigned long arg)
457 struct vme_master master;
458 struct vme_slave slave;
459 struct vme_irq_id irq_req;
460 unsigned long copied;
461 unsigned int minor = MINOR(inode->i_rdev);
464 void __user *argp = (void __user *)arg;
468 switch (type[minor]) {
472 copied = copy_from_user(&irq_req, argp,
473 sizeof(struct vme_irq_id));
475 pr_warn("Partial copy from userspace\n");
479 return vme_irq_generate(vme_user_bridge,
487 memset(&master, 0, sizeof(struct vme_master));
489 /* XXX We do not want to push aspace, cycle and width
490 * to userspace as they are
492 retval = vme_master_get(image[minor].resource,
493 &master.enable, &master.vme_addr,
494 &master.size, &master.aspace,
495 &master.cycle, &master.dwidth);
497 copied = copy_to_user(argp, &master,
498 sizeof(struct vme_master));
500 pr_warn("Partial copy to userspace\n");
508 if (image[minor].mmap_count != 0) {
509 pr_warn("Can't adjust mapped window\n");
513 copied = copy_from_user(&master, argp, sizeof(master));
515 pr_warn("Partial copy from userspace\n");
519 /* XXX We do not want to push aspace, cycle and width
520 * to userspace as they are
522 return vme_master_set(image[minor].resource,
523 master.enable, master.vme_addr, master.size,
524 master.aspace, master.cycle, master.dwidth);
532 memset(&slave, 0, sizeof(struct vme_slave));
534 /* XXX We do not want to push aspace, cycle and width
535 * to userspace as they are
537 retval = vme_slave_get(image[minor].resource,
538 &slave.enable, &slave.vme_addr,
539 &slave.size, &pci_addr, &slave.aspace,
542 copied = copy_to_user(argp, &slave,
543 sizeof(struct vme_slave));
545 pr_warn("Partial copy to userspace\n");
553 copied = copy_from_user(&slave, argp, sizeof(slave));
555 pr_warn("Partial copy from userspace\n");
559 /* XXX We do not want to push aspace, cycle and width
560 * to userspace as they are
562 return vme_slave_set(image[minor].resource,
563 slave.enable, slave.vme_addr, slave.size,
564 image[minor].pci_buf, slave.aspace,
576 vme_user_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
579 struct inode *inode = file_inode(file);
580 unsigned int minor = MINOR(inode->i_rdev);
582 mutex_lock(&image[minor].mutex);
583 ret = vme_user_ioctl(inode, file, cmd, arg);
584 mutex_unlock(&image[minor].mutex);
589 static void vme_user_vm_open(struct vm_area_struct *vma)
591 struct vme_user_vma_priv *vma_priv = vma->vm_private_data;
593 atomic_inc(&vma_priv->refcnt);
596 static void vme_user_vm_close(struct vm_area_struct *vma)
598 struct vme_user_vma_priv *vma_priv = vma->vm_private_data;
599 unsigned int minor = vma_priv->minor;
601 if (!atomic_dec_and_test(&vma_priv->refcnt))
604 mutex_lock(&image[minor].mutex);
605 image[minor].mmap_count--;
606 mutex_unlock(&image[minor].mutex);
611 static int vme_user_master_mmap(unsigned int minor, struct vm_area_struct *vma)
614 struct vme_user_vma_priv *vma_priv;
616 mutex_lock(&image[minor].mutex);
618 err = vme_master_mmap(image[minor].resource, vma);
620 mutex_unlock(&image[minor].mutex);
624 vma_priv = kmalloc(sizeof(struct vme_user_vma_priv), GFP_KERNEL);
625 if (vma_priv == NULL) {
626 mutex_unlock(&image[minor].mutex);
630 vma_priv->minor = minor;
631 atomic_set(&vma_priv->refcnt, 1);
632 vma->vm_ops = &vme_user_vm_ops;
633 vma->vm_private_data = vma_priv;
635 image[minor].mmap_count++;
637 mutex_unlock(&image[minor].mutex);
642 static int vme_user_mmap(struct file *file, struct vm_area_struct *vma)
644 unsigned int minor = MINOR(file_inode(file)->i_rdev);
646 if (type[minor] == MASTER_MINOR)
647 return vme_user_master_mmap(minor, vma);
654 * Unallocate a previously allocated buffer
656 static void buf_unalloc(int num)
658 if (image[num].kern_buf) {
660 pr_debug("UniverseII:Releasing buffer at %p\n",
664 vme_free_consistent(image[num].resource, image[num].size_buf,
665 image[num].kern_buf, image[num].pci_buf);
667 image[num].kern_buf = NULL;
668 image[num].pci_buf = 0;
669 image[num].size_buf = 0;
673 pr_debug("UniverseII: Buffer not allocated\n");
678 static struct vme_driver vme_user_driver = {
680 .match = vme_user_match,
681 .probe = vme_user_probe,
682 .remove = vme_user_remove,
686 static int __init vme_user_init(void)
690 pr_info("VME User Space Access Driver\n");
693 pr_err("No cards, skipping registration\n");
698 /* Let's start by supporting one bus, we can support more than one
699 * in future revisions if that ever becomes necessary.
701 if (bus_num > VME_USER_BUS_MAX) {
702 pr_err("Driver only able to handle %d buses\n",
704 bus_num = VME_USER_BUS_MAX;
708 * Here we just register the maximum number of devices we can and
709 * leave vme_user_match() to allow only 1 to go through to probe().
710 * This way, if we later want to allow multiple user access devices,
711 * we just change the code in vme_user_match().
713 retval = vme_register_driver(&vme_user_driver, VME_MAX_SLOTS);
724 static int vme_user_match(struct vme_dev *vdev)
728 int cur_bus = vme_bus_num(vdev);
729 int cur_slot = vme_slot_num(vdev);
731 for (i = 0; i < bus_num; i++)
732 if ((cur_bus == bus[i]) && (cur_slot == vdev->num))
739 * In this simple access driver, the old behaviour is being preserved as much
740 * as practical. We will therefore reserve the buffers and request the images
741 * here so that we don't have to do it later.
743 static int vme_user_probe(struct vme_dev *vdev)
748 /* Save pointer to the bridge device */
749 if (vme_user_bridge != NULL) {
750 dev_err(&vdev->dev, "Driver can only be loaded for 1 device\n");
754 vme_user_bridge = vdev;
756 /* Initialise descriptors */
757 for (i = 0; i < VME_DEVS; i++) {
758 image[i].kern_buf = NULL;
759 image[i].pci_buf = 0;
760 mutex_init(&image[i].mutex);
761 image[i].device = NULL;
762 image[i].resource = NULL;
766 /* Initialise statistics counters */
769 /* Assign major and minor numbers for the driver */
770 err = register_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS,
773 dev_warn(&vdev->dev, "Error getting Major Number %d for driver.\n",
778 /* Register the driver as a char device */
779 vme_user_cdev = cdev_alloc();
780 if (!vme_user_cdev) {
784 vme_user_cdev->ops = &vme_user_fops;
785 vme_user_cdev->owner = THIS_MODULE;
786 err = cdev_add(vme_user_cdev, MKDEV(VME_MAJOR, 0), VME_DEVS);
788 dev_warn(&vdev->dev, "cdev_all failed\n");
792 /* Request slave resources and allocate buffers (128kB wide) */
793 for (i = SLAVE_MINOR; i < (SLAVE_MAX + 1); i++) {
794 /* XXX Need to properly request attributes */
795 /* For ca91cx42 bridge there are only two slave windows
796 * supporting A16 addressing, so we request A24 supported
799 image[i].resource = vme_slave_request(vme_user_bridge,
801 if (image[i].resource == NULL) {
803 "Unable to allocate slave resource\n");
807 image[i].size_buf = PCI_BUF_SIZE;
808 image[i].kern_buf = vme_alloc_consistent(image[i].resource,
809 image[i].size_buf, &image[i].pci_buf);
810 if (image[i].kern_buf == NULL) {
812 "Unable to allocate memory for buffer\n");
813 image[i].pci_buf = 0;
814 vme_slave_free(image[i].resource);
821 * Request master resources allocate page sized buffers for small
824 for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++) {
825 /* XXX Need to properly request attributes */
826 image[i].resource = vme_master_request(vme_user_bridge,
827 VME_A32, VME_SCT, VME_D32);
828 if (image[i].resource == NULL) {
830 "Unable to allocate master resource\n");
834 image[i].size_buf = PCI_BUF_SIZE;
835 image[i].kern_buf = kmalloc(image[i].size_buf, GFP_KERNEL);
836 if (image[i].kern_buf == NULL) {
838 vme_master_free(image[i].resource);
843 /* Create sysfs entries - on udev systems this creates the dev files */
844 vme_user_sysfs_class = class_create(THIS_MODULE, driver_name);
845 if (IS_ERR(vme_user_sysfs_class)) {
846 dev_err(&vdev->dev, "Error creating vme_user class.\n");
847 err = PTR_ERR(vme_user_sysfs_class);
851 /* Add sysfs Entries */
852 for (i = 0; i < VME_DEVS; i++) {
857 name = "bus/vme/m%d";
860 name = "bus/vme/ctl";
863 name = "bus/vme/s%d";
870 num = (type[i] == SLAVE_MINOR) ? i - (MASTER_MAX + 1) : i;
871 image[i].device = device_create(vme_user_sysfs_class, NULL,
872 MKDEV(VME_MAJOR, i), NULL, name, num);
873 if (IS_ERR(image[i].device)) {
874 dev_info(&vdev->dev, "Error creating sysfs device\n");
875 err = PTR_ERR(image[i].device);
885 device_destroy(vme_user_sysfs_class, MKDEV(VME_MAJOR, i));
887 class_destroy(vme_user_sysfs_class);
889 /* Ensure counter set correcty to unalloc all master windows */
892 while (i > MASTER_MINOR) {
894 kfree(image[i].kern_buf);
895 vme_master_free(image[i].resource);
899 * Ensure counter set correcty to unalloc all slave windows and buffers
903 while (i > SLAVE_MINOR) {
906 vme_slave_free(image[i].resource);
909 cdev_del(vme_user_cdev);
911 unregister_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS);
917 static int vme_user_remove(struct vme_dev *dev)
921 /* Remove sysfs Entries */
922 for (i = 0; i < VME_DEVS; i++) {
923 mutex_destroy(&image[i].mutex);
924 device_destroy(vme_user_sysfs_class, MKDEV(VME_MAJOR, i));
926 class_destroy(vme_user_sysfs_class);
928 for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++) {
929 kfree(image[i].kern_buf);
930 vme_master_free(image[i].resource);
933 for (i = SLAVE_MINOR; i < (SLAVE_MAX + 1); i++) {
934 vme_slave_set(image[i].resource, 0, 0, 0, 0, VME_A32, 0);
936 vme_slave_free(image[i].resource);
939 /* Unregister device driver */
940 cdev_del(vme_user_cdev);
942 /* Unregiser the major and minor device numbers */
943 unregister_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS);
948 static void __exit vme_user_exit(void)
950 vme_unregister_driver(&vme_user_driver);
954 MODULE_PARM_DESC(bus, "Enumeration of VMEbus to which the driver is connected");
955 module_param_array(bus, int, &bus_num, 0);
957 MODULE_DESCRIPTION("VME User Space Access Driver");
958 MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com");
959 MODULE_LICENSE("GPL");
961 module_init(vme_user_init);
962 module_exit(vme_user_exit);