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/cdev.h>
21 #include <linux/delay.h>
22 #include <linux/device.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/errno.h>
25 #include <linux/init.h>
26 #include <linux/ioctl.h>
27 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/pagemap.h>
31 #include <linux/pci.h>
32 #include <linux/mutex.h>
33 #include <linux/slab.h>
34 #include <linux/spinlock.h>
35 #include <linux/syscalls.h>
36 #include <linux/types.h>
39 #include <linux/uaccess.h>
40 #include <linux/vme.h>
44 static DEFINE_MUTEX(vme_user_mutex);
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 */
104 static struct image_desc image[VME_DEVS];
106 struct driver_stats {
108 unsigned long writes;
109 unsigned long ioctls;
112 unsigned long dmaerrors;
113 unsigned long timeouts;
114 unsigned long external;
116 static struct driver_stats statistics;
118 static struct cdev *vme_user_cdev; /* Character device */
119 static struct class *vme_user_sysfs_class; /* Sysfs class */
120 static struct vme_dev *vme_user_bridge; /* Pointer to user device */
123 static const int type[VME_DEVS] = { MASTER_MINOR, MASTER_MINOR,
124 MASTER_MINOR, MASTER_MINOR,
125 SLAVE_MINOR, SLAVE_MINOR,
126 SLAVE_MINOR, SLAVE_MINOR,
131 static int vme_user_open(struct inode *, struct file *);
132 static int vme_user_release(struct inode *, struct file *);
133 static ssize_t vme_user_read(struct file *, char __user *, size_t, loff_t *);
134 static ssize_t vme_user_write(struct file *, const char __user *, size_t,
136 static loff_t vme_user_llseek(struct file *, loff_t, int);
137 static long vme_user_unlocked_ioctl(struct file *, unsigned int, unsigned long);
139 static int vme_user_match(struct vme_dev *);
140 static int vme_user_probe(struct vme_dev *);
141 static int vme_user_remove(struct vme_dev *);
143 static const struct file_operations vme_user_fops = {
144 .open = vme_user_open,
145 .release = vme_user_release,
146 .read = vme_user_read,
147 .write = vme_user_write,
148 .llseek = vme_user_llseek,
149 .unlocked_ioctl = vme_user_unlocked_ioctl,
150 .compat_ioctl = vme_user_unlocked_ioctl,
155 * Reset all the statistic counters
157 static void reset_counters(void)
159 statistics.reads = 0;
160 statistics.writes = 0;
161 statistics.ioctls = 0;
163 statistics.berrs = 0;
164 statistics.dmaerrors = 0;
165 statistics.timeouts = 0;
168 static int vme_user_open(struct inode *inode, struct file *file)
171 unsigned int minor = MINOR(inode->i_rdev);
173 mutex_lock(&image[minor].mutex);
174 /* Allow device to be opened if a resource is needed and allocated. */
175 if (minor < CONTROL_MINOR && image[minor].resource == NULL) {
176 pr_err("No resources allocated for device\n");
181 /* Increment user count */
182 image[minor].users++;
184 mutex_unlock(&image[minor].mutex);
189 mutex_unlock(&image[minor].mutex);
194 static int vme_user_release(struct inode *inode, struct file *file)
196 unsigned int minor = MINOR(inode->i_rdev);
198 mutex_lock(&image[minor].mutex);
200 /* Decrement user count */
201 image[minor].users--;
203 mutex_unlock(&image[minor].mutex);
209 * We are going ot alloc a page during init per window for small transfers.
210 * Small transfers will go VME -> buffer -> user space. Larger (more than a
211 * page) transfers will lock the user space buffer into memory and then
212 * transfer the data directly into the user space buffers.
214 static ssize_t resource_to_user(int minor, char __user *buf, size_t count,
220 if (count <= image[minor].size_buf) {
221 /* We copy to kernel buffer */
222 copied = vme_master_read(image[minor].resource,
223 image[minor].kern_buf, count, *ppos);
227 retval = __copy_to_user(buf, image[minor].kern_buf,
228 (unsigned long)copied);
230 copied = (copied - retval);
231 pr_info("User copy failed\n");
236 /* XXX Need to write this */
237 pr_info("Currently don't support large transfers\n");
238 /* Map in pages from userspace */
240 /* Call vme_master_read to do the transfer */
248 * We are going to alloc a page during init per window for small transfers.
249 * Small transfers will go user space -> buffer -> VME. Larger (more than a
250 * page) transfers will lock the user space buffer into memory and then
251 * transfer the data directly from the user space buffers out to VME.
253 static ssize_t resource_from_user(unsigned int minor, const char __user *buf,
254 size_t count, loff_t *ppos)
259 if (count <= image[minor].size_buf) {
260 retval = __copy_from_user(image[minor].kern_buf, buf,
261 (unsigned long)count);
263 copied = (copied - retval);
267 copied = vme_master_write(image[minor].resource,
268 image[minor].kern_buf, copied, *ppos);
270 /* XXX Need to write this */
271 pr_info("Currently don't support large transfers\n");
272 /* Map in pages from userspace */
274 /* Call vme_master_write to do the transfer */
281 static ssize_t buffer_to_user(unsigned int minor, char __user *buf,
282 size_t count, loff_t *ppos)
287 image_ptr = image[minor].kern_buf + *ppos;
289 retval = __copy_to_user(buf, image_ptr, (unsigned long)count);
291 retval = (count - retval);
292 pr_warn("Partial copy to userspace\n");
296 /* Return number of bytes successfully read */
300 static ssize_t buffer_from_user(unsigned int minor, const char __user *buf,
301 size_t count, loff_t *ppos)
306 image_ptr = image[minor].kern_buf + *ppos;
308 retval = __copy_from_user(image_ptr, buf, (unsigned long)count);
310 retval = (count - retval);
311 pr_warn("Partial copy to userspace\n");
315 /* Return number of bytes successfully read */
319 static ssize_t vme_user_read(struct file *file, char __user *buf, size_t count,
322 unsigned int minor = MINOR(file_inode(file)->i_rdev);
327 if (minor == CONTROL_MINOR)
330 mutex_lock(&image[minor].mutex);
332 /* XXX Do we *really* want this helper - we can use vme_*_get ? */
333 image_size = vme_get_size(image[minor].resource);
335 /* Ensure we are starting at a valid location */
336 if ((*ppos < 0) || (*ppos > (image_size - 1))) {
337 mutex_unlock(&image[minor].mutex);
341 /* Ensure not reading past end of the image */
342 if (*ppos + count > image_size)
343 okcount = image_size - *ppos;
347 switch (type[minor]) {
349 retval = resource_to_user(minor, buf, okcount, ppos);
352 retval = buffer_to_user(minor, buf, okcount, ppos);
358 mutex_unlock(&image[minor].mutex);
365 static ssize_t vme_user_write(struct file *file, const char __user *buf,
366 size_t count, loff_t *ppos)
368 unsigned int minor = MINOR(file_inode(file)->i_rdev);
373 if (minor == CONTROL_MINOR)
376 mutex_lock(&image[minor].mutex);
378 image_size = vme_get_size(image[minor].resource);
380 /* Ensure we are starting at a valid location */
381 if ((*ppos < 0) || (*ppos > (image_size - 1))) {
382 mutex_unlock(&image[minor].mutex);
386 /* Ensure not reading past end of the image */
387 if (*ppos + count > image_size)
388 okcount = image_size - *ppos;
392 switch (type[minor]) {
394 retval = resource_from_user(minor, buf, okcount, ppos);
397 retval = buffer_from_user(minor, buf, okcount, ppos);
403 mutex_unlock(&image[minor].mutex);
411 static loff_t vme_user_llseek(struct file *file, loff_t off, int whence)
413 loff_t absolute = -1;
414 unsigned int minor = MINOR(file_inode(file)->i_rdev);
417 if (minor == CONTROL_MINOR)
420 mutex_lock(&image[minor].mutex);
421 image_size = vme_get_size(image[minor].resource);
428 absolute = file->f_pos + off;
431 absolute = image_size + off;
434 mutex_unlock(&image[minor].mutex);
438 if ((absolute < 0) || (absolute >= image_size)) {
439 mutex_unlock(&image[minor].mutex);
443 file->f_pos = absolute;
445 mutex_unlock(&image[minor].mutex);
451 * The ioctls provided by the old VME access method (the one at vmelinux.org)
452 * are most certainly wrong as the effectively push the registers layout
453 * through to user space. Given that the VME core can handle multiple bridges,
454 * with different register layouts this is most certainly not the way to go.
456 * We aren't using the structures defined in the Motorola driver either - these
457 * are also quite low level, however we should use the definitions that have
458 * already been defined.
460 static int vme_user_ioctl(struct inode *inode, struct file *file,
461 unsigned int cmd, unsigned long arg)
463 struct vme_master master;
464 struct vme_slave slave;
465 struct vme_irq_id irq_req;
466 unsigned long copied;
467 unsigned int minor = MINOR(inode->i_rdev);
470 void __user *argp = (void __user *)arg;
474 switch (type[minor]) {
478 copied = copy_from_user(&irq_req, argp,
479 sizeof(struct vme_irq_id));
481 pr_warn("Partial copy from userspace\n");
485 return vme_irq_generate(vme_user_bridge,
493 memset(&master, 0, sizeof(struct vme_master));
495 /* XXX We do not want to push aspace, cycle and width
496 * to userspace as they are
498 retval = vme_master_get(image[minor].resource,
499 &master.enable, &master.vme_addr,
500 &master.size, &master.aspace,
501 &master.cycle, &master.dwidth);
503 copied = copy_to_user(argp, &master,
504 sizeof(struct vme_master));
506 pr_warn("Partial copy to userspace\n");
514 copied = copy_from_user(&master, argp, sizeof(master));
516 pr_warn("Partial copy from userspace\n");
520 /* XXX We do not want to push aspace, cycle and width
521 * to userspace as they are
523 return vme_master_set(image[minor].resource,
524 master.enable, master.vme_addr, master.size,
525 master.aspace, master.cycle, master.dwidth);
533 memset(&slave, 0, sizeof(struct vme_slave));
535 /* XXX We do not want to push aspace, cycle and width
536 * to userspace as they are
538 retval = vme_slave_get(image[minor].resource,
539 &slave.enable, &slave.vme_addr,
540 &slave.size, &pci_addr, &slave.aspace,
543 copied = copy_to_user(argp, &slave,
544 sizeof(struct vme_slave));
546 pr_warn("Partial copy to userspace\n");
554 copied = copy_from_user(&slave, argp, sizeof(slave));
556 pr_warn("Partial copy from userspace\n");
560 /* XXX We do not want to push aspace, cycle and width
561 * to userspace as they are
563 return vme_slave_set(image[minor].resource,
564 slave.enable, slave.vme_addr, slave.size,
565 image[minor].pci_buf, slave.aspace,
577 vme_user_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
581 mutex_lock(&vme_user_mutex);
582 ret = vme_user_ioctl(file_inode(file), file, cmd, arg);
583 mutex_unlock(&vme_user_mutex);
590 * Unallocate a previously allocated buffer
592 static void buf_unalloc(int num)
594 if (image[num].kern_buf) {
596 pr_debug("UniverseII:Releasing buffer at %p\n",
600 vme_free_consistent(image[num].resource, image[num].size_buf,
601 image[num].kern_buf, image[num].pci_buf);
603 image[num].kern_buf = NULL;
604 image[num].pci_buf = 0;
605 image[num].size_buf = 0;
609 pr_debug("UniverseII: Buffer not allocated\n");
614 static struct vme_driver vme_user_driver = {
616 .match = vme_user_match,
617 .probe = vme_user_probe,
618 .remove = vme_user_remove,
622 static int __init vme_user_init(void)
626 pr_info("VME User Space Access Driver\n");
629 pr_err("No cards, skipping registration\n");
634 /* Let's start by supporting one bus, we can support more than one
635 * in future revisions if that ever becomes necessary.
637 if (bus_num > VME_USER_BUS_MAX) {
638 pr_err("Driver only able to handle %d buses\n",
640 bus_num = VME_USER_BUS_MAX;
644 * Here we just register the maximum number of devices we can and
645 * leave vme_user_match() to allow only 1 to go through to probe().
646 * This way, if we later want to allow multiple user access devices,
647 * we just change the code in vme_user_match().
649 retval = vme_register_driver(&vme_user_driver, VME_MAX_SLOTS);
660 static int vme_user_match(struct vme_dev *vdev)
664 int cur_bus = vme_bus_num(vdev);
665 int cur_slot = vme_slot_num(vdev);
667 for (i = 0; i < bus_num; i++)
668 if ((cur_bus == bus[i]) && (cur_slot == vdev->num))
675 * In this simple access driver, the old behaviour is being preserved as much
676 * as practical. We will therefore reserve the buffers and request the images
677 * here so that we don't have to do it later.
679 static int vme_user_probe(struct vme_dev *vdev)
684 /* Save pointer to the bridge device */
685 if (vme_user_bridge != NULL) {
686 dev_err(&vdev->dev, "Driver can only be loaded for 1 device\n");
690 vme_user_bridge = vdev;
692 /* Initialise descriptors */
693 for (i = 0; i < VME_DEVS; i++) {
694 image[i].kern_buf = NULL;
695 image[i].pci_buf = 0;
696 mutex_init(&image[i].mutex);
697 image[i].device = NULL;
698 image[i].resource = NULL;
702 /* Initialise statistics counters */
705 /* Assign major and minor numbers for the driver */
706 err = register_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS,
709 dev_warn(&vdev->dev, "Error getting Major Number %d for driver.\n",
714 /* Register the driver as a char device */
715 vme_user_cdev = cdev_alloc();
716 if (!vme_user_cdev) {
720 vme_user_cdev->ops = &vme_user_fops;
721 vme_user_cdev->owner = THIS_MODULE;
722 err = cdev_add(vme_user_cdev, MKDEV(VME_MAJOR, 0), VME_DEVS);
724 dev_warn(&vdev->dev, "cdev_all failed\n");
728 /* Request slave resources and allocate buffers (128kB wide) */
729 for (i = SLAVE_MINOR; i < (SLAVE_MAX + 1); i++) {
730 /* XXX Need to properly request attributes */
731 /* For ca91cx42 bridge there are only two slave windows
732 * supporting A16 addressing, so we request A24 supported
735 image[i].resource = vme_slave_request(vme_user_bridge,
737 if (image[i].resource == NULL) {
739 "Unable to allocate slave resource\n");
743 image[i].size_buf = PCI_BUF_SIZE;
744 image[i].kern_buf = vme_alloc_consistent(image[i].resource,
745 image[i].size_buf, &image[i].pci_buf);
746 if (image[i].kern_buf == NULL) {
748 "Unable to allocate memory for buffer\n");
749 image[i].pci_buf = 0;
750 vme_slave_free(image[i].resource);
757 * Request master resources allocate page sized buffers for small
760 for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++) {
761 /* XXX Need to properly request attributes */
762 image[i].resource = vme_master_request(vme_user_bridge,
763 VME_A32, VME_SCT, VME_D32);
764 if (image[i].resource == NULL) {
766 "Unable to allocate master resource\n");
770 image[i].size_buf = PCI_BUF_SIZE;
771 image[i].kern_buf = kmalloc(image[i].size_buf, GFP_KERNEL);
772 if (image[i].kern_buf == NULL) {
774 vme_master_free(image[i].resource);
779 /* Create sysfs entries - on udev systems this creates the dev files */
780 vme_user_sysfs_class = class_create(THIS_MODULE, driver_name);
781 if (IS_ERR(vme_user_sysfs_class)) {
782 dev_err(&vdev->dev, "Error creating vme_user class.\n");
783 err = PTR_ERR(vme_user_sysfs_class);
787 /* Add sysfs Entries */
788 for (i = 0; i < VME_DEVS; i++) {
793 name = "bus/vme/m%d";
796 name = "bus/vme/ctl";
799 name = "bus/vme/s%d";
806 num = (type[i] == SLAVE_MINOR) ? i - (MASTER_MAX + 1) : i;
807 image[i].device = device_create(vme_user_sysfs_class, NULL,
808 MKDEV(VME_MAJOR, i), NULL, name, num);
809 if (IS_ERR(image[i].device)) {
810 dev_info(&vdev->dev, "Error creating sysfs device\n");
811 err = PTR_ERR(image[i].device);
821 device_destroy(vme_user_sysfs_class, MKDEV(VME_MAJOR, i));
823 class_destroy(vme_user_sysfs_class);
825 /* Ensure counter set correcty to unalloc all master windows */
828 while (i > MASTER_MINOR) {
830 kfree(image[i].kern_buf);
831 vme_master_free(image[i].resource);
835 * Ensure counter set correcty to unalloc all slave windows and buffers
839 while (i > SLAVE_MINOR) {
842 vme_slave_free(image[i].resource);
845 cdev_del(vme_user_cdev);
847 unregister_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS);
853 static int vme_user_remove(struct vme_dev *dev)
857 /* Remove sysfs Entries */
858 for (i = 0; i < VME_DEVS; i++) {
859 mutex_destroy(&image[i].mutex);
860 device_destroy(vme_user_sysfs_class, MKDEV(VME_MAJOR, i));
862 class_destroy(vme_user_sysfs_class);
864 for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++) {
865 kfree(image[i].kern_buf);
866 vme_master_free(image[i].resource);
869 for (i = SLAVE_MINOR; i < (SLAVE_MAX + 1); i++) {
870 vme_slave_set(image[i].resource, 0, 0, 0, 0, VME_A32, 0);
872 vme_slave_free(image[i].resource);
875 /* Unregister device driver */
876 cdev_del(vme_user_cdev);
878 /* Unregiser the major and minor device numbers */
879 unregister_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS);
884 static void __exit vme_user_exit(void)
886 vme_unregister_driver(&vme_user_driver);
890 MODULE_PARM_DESC(bus, "Enumeration of VMEbus to which the driver is connected");
891 module_param_array(bus, int, &bus_num, 0);
893 MODULE_DESCRIPTION("VME User Space Access Driver");
894 MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com");
895 MODULE_LICENSE("GPL");
897 module_init(vme_user_init);
898 module_exit(vme_user_exit);